package ttestClosed;

import java.util.concurrent.TimeUnit;

import station.DataMessage;
import ttestExecuter.CostumScheduledExecuter;
import ttestExecuter.CostumThreadFactory;
import ttestExecuter.SlotExecuter;
import ttestExecuter.TestSender;

public class TestChannel implements Runnable {

//	private Frame timeFrame;
//	private TimeSyncer syncer;
//	private byte nextSlot = 0;
	private boolean isRunning = true;
	private static SlotExecuter sExecInstance = new SlotExecuter();
	
	public static SlotExecuter getInstance() {
		return sExecInstance;
	}
	
	private TestChannel() {
//		this.syncer = new TimeSyncer();
	}
	
	@Override
	public void run() {
		CostumScheduledExecuter cse = new CostumScheduledExecuter(10, new CostumThreadFactory());
		
//		this.timeFrame = new Frame(System.currentTimeMillis());
		long standardDelay = 50;
		long syncTuning = 0; //sync difference goes here!
			
		System.out.println("Start Threads!");
		cse.scheduleWithFixedDelay(new TestSender(), 25, standardDelay, TimeUnit.MILLISECONDS);
		while (isRunning ) {
			if (cse.getTaskCount() == 20L) {
				cse.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
				cse.shutdown();
				isRunning = false;
			}
		}
//		syncTuning = syncer.syncTime(); //sync time over NTP!
		standardDelay -= syncTuning;
	}
	
//	public DataMessage nextMsg() {
//		DataMessage dataMsg = timeFrame.getSlots()[nextSlot++].getMessage();
//		dataMsg.setNextSlot(nextSlot);
//		return dataMsg;
//	}
	
	public void putMsg(DataMessage dataMsg, int idx) {	 // calculate idx time /1000 %1000! 
//		timeFrame.insert(new Slot(dataMsg), idx);
	}
	
}
